<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Auth {
	private static $ci;
	function __construct() {
	    self::$ci = get_instance();
	} //end __contruct()
	
	public static function login($username, $password) {
		self::$ci->load->database();
		self::$ci->db->select("user_id");
		self::$ci->db->select("type");
		self::$ci->db->from("user");
		self::$ci->db->where("username",$username);
		self::$ci->db->where("password",md5($password));
		self::$ci->db->where("is_active","Y");
		$query = self::$ci->db->get();
		if($query->num_rows() > 0){
			$user = $query->row_array();
			self::$ci->session->set_userdata('user_id', $user['user_id']);
			self::$ci->session->set_userdata('type_user', $user['type']);
			
			$data = array(
			   'last_logged_in' => date("Y-m-d H:i:s")
			);
			self::$ci->db->where("user_id",$user['user_id']);
			self::$ci->db->update('user', $data); 
			return true;
		}else{
			return false;
		}
		
	}
	
	public static function logout() {
		self::$ci->session->sess_destroy();
		redirect("/");
	}
	
	public static function isLoggedIn(){
		if(self::$ci->session->userdata("user_id") ){
			return true;
		}else{
			return false;
		}
	}
	
}